package org.dromara.utils.utils;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.Nyt7PolicyInsurance.domain.Nyt7PolicyInsurance;
import org.dromara.Nyt7PolicyInsurance.mapper.Nyt7PolicyInsuranceMapper;
import org.dromara.common.tenant.core.TenantEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Supplier;

public class FilterUtil {

    public static List<Nyt7PolicyInsurance> getInsurances(StatisticsBaseEntity bo, Nyt7PolicyInsuranceMapper nyt7PolicyInsuranceMapper, String X) {
        //根据保险公司/地区进行筛选
        if (X.equals("company")){
            List<Nyt7PolicyInsurance> policyInsurances = CreateAndFilter(
                bo,
                nyt7PolicyInsuranceMapper::selectEx,
                () -> nyt7PolicyInsuranceMapper.selectByPostNameEx('%' + bo.getCompany() + '%')
            );
            policyInsurances = BeanUtil.copyToList(policyInsurances, Nyt7PolicyInsurance.class);
            //合并重复保险公司名称的保单
            if (!policyInsurances.isEmpty()) {
                List<Nyt7PolicyInsurance> removeList = new ArrayList<>();
                Map<String,Nyt7PolicyInsurance> map = new HashMap<>();
                for (Nyt7PolicyInsurance nyt7PolicyInsurance : policyInsurances) {
                    String postName = nyt7PolicyInsurance.getSysPost().getPostName();
                    if(map.containsKey(postName)){
                        Nyt7PolicyInsurance nyt7PolicyInsurance1 = map.get(postName);
                        nyt7PolicyInsurance1.addFields(nyt7PolicyInsurance);
                        removeList.add(nyt7PolicyInsurance);
                    }else{
                        map.put(postName,nyt7PolicyInsurance);
                    }
                }
                policyInsurances.removeAll(removeList);
            }
            return policyInsurances;
        }else if (X.equals("region")){
            List<Nyt7PolicyInsurance> policyInsurances = CreateAndFilter(
                bo,
                nyt7PolicyInsuranceMapper::selectEx,
                () -> nyt7PolicyInsuranceMapper.selectByCityEx('%' + bo.getRegion() + '%')
            );
            policyInsurances = BeanUtil.copyToList(policyInsurances, Nyt7PolicyInsurance.class);
            //合并重复地区的保单
            if (!policyInsurances.isEmpty()) {
                List<Nyt7PolicyInsurance> removeList = new ArrayList<>();
                Map<String,Nyt7PolicyInsurance> map = new HashMap<>();
                for (Nyt7PolicyInsurance nyt7PolicyInsurance : policyInsurances) {
                    String city = nyt7PolicyInsurance.getPlotDetailInfo().getCity();
                    if(map.containsKey(city)){
                        Nyt7PolicyInsurance nyt7PolicyInsurance1 = map.get(city);
                        nyt7PolicyInsurance1.addFields(nyt7PolicyInsurance);
                        removeList.add(nyt7PolicyInsurance);
                    }else{
                        map.put(city,nyt7PolicyInsurance);
                    }
                }
                policyInsurances.removeAll(removeList);
            }
            return policyInsurances;
        }
        return null;

    }

    private static <T extends TenantEntity> List<T> CreateAndFilter(StatisticsBaseEntity bo, Supplier<List<T>> selectEx, Supplier<List<T>> selectByXEx) {

        //公司筛选
        List<T> entity;
        if (bo.getCompany() == null && bo.getRegion() == null) {
            entity = selectEx.get();
        } else {
            if(bo.getCompany() == null) bo.setCompany("");
            if(bo.getRegion() == null) bo.setRegion("");
            entity = selectByXEx.get();
        }

        //日期筛选
        Map<String, Object> params = bo.getParams();
        String beginCreateTimeStr = (String) params.get("beginCreateTime");
        String endCreateTimeStr = (String) params.get("endCreateTime");

        if (beginCreateTimeStr != null && endCreateTimeStr != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date beginCreateTimeDate;
            Date endCreateTimeDate;
            try {
                beginCreateTimeDate = dateFormat.parse(beginCreateTimeStr);
                endCreateTimeDate = dateFormat.parse(endCreateTimeStr);

                return entity.stream()
                    .filter(v -> v.getCreateTime().after(beginCreateTimeDate)
                        && v.getCreateTime().before(endCreateTimeDate))
                    .toList();
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        return entity;
    }
}
